﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;

namespace Dycox.ComponentModel
{
    public class ModelProperty
    {
        internal protected ModelProperty(PropertyDescriptor descriptor, XElement elt)
        {
            if (descriptor == null)
                throw new ArgumentNullException("descriptor");
            if (elt == null)
                throw new ArgumentNullException("elt");

            string value;

            _displayName = elt.GetAttrString("displayName").TrimToNull() ?? descriptor.DisplayName;
            _description = elt.GetAttrString("description").TrimToNull() ?? descriptor.Description;
            _category = elt.GetAttrString("category").TrimToNull() ?? descriptor.Category;


            value = elt.GetAttrString("converter").TrimToNull();
            if (value != null)
            {
                Type type = Type.GetType(value);
                _converterType = type;
            }

            _descriptor = descriptor;
            _usage = elt.GetAttrEnum<ModelPropertyUsage>("use");
        }

        private ModelPropertyUsage _usage;

        public ModelPropertyUsage Usage
        {
            get { return _usage; }
        }


        private PropertyDescriptor _descriptor;

        public PropertyDescriptor Descriptor
        {
            get { return _descriptor; }
        }

        public string Name
        {
            get { return _descriptor.Name; }
        }


        private string _displayName;

        public string DisplayName
        {
            get { return _displayName; }
        }

        private string _description;

        public string Description
        {
            get { return _description; }
        }

        private string _category;

        public string Category
        {
            get { return _category; }
        }

        private Type _converterType;

        public Type ConverterType
        {
            get { return _converterType; }
        }


    }

}
